package com.github.io.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.io.annotation.Api;
import com.github.io.annotation.ApiOperateNote;
import com.github.io.entity.SysApi;
import com.github.io.service.ISysApiService;
import com.github.io.utils.Constant;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/8/28 13:43
 * Description: 接口Api控制类
 */

@Api(value = "接口控制器")
@RestController
@RequestMapping("/api")
public class SysApiController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ISysApiService sysApiService;
    
    @ApiOperateNote(value = "列表", note = "列表")
    @GetMapping("/list")
    public Object list(SysApi sysApi){
        List<SysApi> list = sysApiService.list();
        return list;
    }

    @ApiOperateNote(value = "分页", note = "分页")
    @GetMapping("/page")
    public Object page(SysApi sysApi, @RequestParam(defaultValue = "0") long current, @RequestParam(defaultValue = "10") long size){
        Page<SysApi> sysApiPage = new Page<>(current, size);
        IPage<SysApi> page = sysApiService.page(sysApiPage);
        return page;
    }

    @ApiOperateNote(value = "同步数据", note = "同步数据")
    @GetMapping("/sync")
    public Object syncApiInfo() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Map<String, Object>> data = (List<Map<String, Object>>) redisTemplate.opsForValue().get(Constant.API_SOURCE_KEY);
            List<SysApi> sysApis = new ArrayList<>();
            if (!ObjectUtils.isEmpty(data)) {

                data.forEach(map -> {
                    SysApi sysApi = new SysApi();
                    sysApi.setMethodName((String) map.get("methodName"));
                    sysApi.setBeanName((String) map.get("beanName"));
                    sysApi.setMethodName((String) map.get("methodName"));
                    sysApi.setMethods((String) map.get("methods"));
                    sysApi.setMethodUrls((String) map.get("methodUrls"));
                    sysApi.setMediaTypes((String) map.get("mediaTypes"));
                    sysApi.setReturnParameterTypeName((String) map.get("returnParameterTypeName"));
                    sysApi.setHandlerNote((String) map.get("handlerNote"));
                    sysApi.setOperateNote((String) map.get("operateNote"));
                    sysApi.setOperateValue((String) map.get("operateValue"));
                    sysApis.add(sysApi);
                });
            }
            sysApiService.saveOrUpdateBatch(sysApis);
            result.put("status", 200);
            result.put("msg", "数据同步成功");
            
            // 清除缓存
            redisTemplate.delete(Constant.API_SOURCE_KEY);
        } catch (Exception e) {
            e.printStackTrace();

            result.put("status", 500);
            result.put("msg", "数据同步异常");
        }
        return result;
    }
}
